package org.crazyit.activiti_d4._3_UserPicture;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Picture;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

public class UserPicture {

    public static void main(String[] args) throws Exception {

        //1. 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2. 得到身份服务组件实例
        IdentityService identityService = engine.getIdentityService();

        //由于上个类已经创建过用户, 这边跳过

        //3. 读取图片并转换为byte 数组
        File file = new File("c6/target/classes/picture.png");
        FileInputStream fis = new FileInputStream(file);
        BufferedImage img = ImageIO.read(fis);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ImageIO.write(img,"png",output);

        //4. 获取图片的byte 数组
        byte[] picArray = output.toByteArray();

        //5. 创建Picture 实例
        Picture picture = new Picture(picArray, "angus image");

        //6. 为用户设置图片
        identityService.setUserPicture("42e737c3-d7ba-4f93-bdd1-11e4c327f7e3",picture);

    }

}
